package kokonut.contact;

import java.io.IOException;
import javax.servlet.http.*;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 *
 * @author markel
 */
public class SendContactServlet extends HttpServlet {

    @Override
    public void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws IOException {

        String name = req.getParameter("name");
        String emailAdress = req.getParameter("emailAdress");
        String contactReason = req.getParameter("contactReason");
        String contactBody = req.getParameter("contactBody");

        Properties props = new Properties();
        Session session = Session.getDefaultInstance(props, null);

        String msgBody = "Nuevo mensaje recibido desde seccion Contacto de Kokonut Webpage\n";
        msgBody += "Enviado por: "+ name + ", con direccion de email: " + emailAdress +"\n";
        msgBody += "Razon del mensaje: "+ contactReason +"\n";
        msgBody += "Cuerpo del mensaje:\n";
        msgBody += contactBody;
        try {
            Message msg = new MimeMessage(session);
            msg.setFrom(new InternetAddress("admin@example.com", "Example.com Admin"));
            msg.addRecipient(Message.RecipientType.TO,
                    new InternetAddress("markel.arizaga@gmail.com", "Mr. Markel"));
            msg.setSubject("New message from the kokonut web");
            msg.setText(msgBody);
            Transport.send(msg);
            resp.sendRedirect("/");
        } catch (AddressException e) {
            // ...
        } catch (MessagingException e) {
            // ...
        }
    }
}